﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TSIG.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Globalization;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The TSIG record.
    /// </summary>
    public sealed class TSIG : Record
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TSIG"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public TSIG(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            this.AlgorithmName = recordReader.ReadDomainName();
            this.TimeSigned = recordReader.ReadUInt32() << 32 | recordReader.ReadUInt32();
            this.Fudge = recordReader.ReadUInt16();
            this.MacSize = recordReader.ReadUInt16();
            this.Mac = recordReader.ReadBytes(this.MacSize);
            this.OriginalId = recordReader.ReadUInt16();
            this.Error = recordReader.ReadUInt16();
            this.OtherLen = recordReader.ReadUInt16();
            this.OtherData = recordReader.ReadBytes(this.OtherLen);
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="TSIG" /> class.
        /// </summary>
        public TSIG()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Algorithm name.
        /// </summary>
        public string AlgorithmName { get; private set; }

        /// <summary>
        ///     Gets Error.
        /// </summary>
        public ushort Error { get; private set; }

        /// <summary>
        ///     Gets Fudge.
        /// </summary>
        public ushort Fudge { get; private set; }

        /// <summary>
        ///     Gets Mac.
        /// </summary>
        public IEnumerable<byte> Mac { get; private set; }

        /// <summary>
        ///     Gets Mac size.
        /// </summary>
        public ushort MacSize { get; private set; }

        /// <summary>
        ///     Gets Original id.
        /// </summary>
        public ushort OriginalId { get; private set; }

        /// <summary>
        ///     Gets Other data.
        /// </summary>
        public IEnumerable<byte> OtherData { get; private set; }

        /// <summary>
        ///     Gets Other len.
        /// </summary>
        public ushort OtherLen { get; private set; }

        /// <summary>
        ///     Gets Time signed.
        /// </summary>
        public long TimeSigned { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dateTime = dateTime.AddSeconds(this.TimeSigned);
            var printDate = dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString();
            return string.Format(
                CultureInfo.InvariantCulture, 
                "{0} {1} {2} {3} {4}", 
                this.AlgorithmName, 
                printDate, 
                this.Fudge, 
                this.OriginalId, 
                this.Error);
        }

        #endregion
    }
}